package com.demo.springcloud.controller;

import com.demo.springcloud.lb.LoadBalancer;
import com.demo.springcloud.pojo.CommonResult;
import com.demo.springcloud.pojo.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @author chenyuan
 * @date 2020/11/30 22:40
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OrderController {
	private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 引入 自定义 的 LB
	 */
	@Autowired
	private LoadBalancer loadBalancer;

	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("/getPaymentById/{id}")
	public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
		return restTemplate.getForObject(PAYMENT_URL + "/provider/getPaymentById/" + id, CommonResult.class);
	}

	@GetMapping("/getForEntity/{id}")
	public CommonResult<Payment> getPaymentById2(@PathVariable("id") Long id) {
		ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/provider/getPaymentById/" + id, CommonResult.class);

		if (entity.getStatusCode().is2xxSuccessful()) {
			//这个 实体对象 中还包含其他很多信息
			log.info("{状态码: " + entity.getStatusCode() +
					", 头信息: " + entity.getHeaders() + "}");
			// 如果成功
			return entity.getBody();
		} else {
			return new CommonResult<>(444, "【查询失败】");
		}
	}

	@GetMapping("/addPayment")
	public CommonResult<Payment> addPayment(Payment payment) {
		log.info("[=插入数据=]{" + payment + "}");
		return restTemplate.postForObject(PAYMENT_URL + "/provider/addPayment", payment, CommonResult.class);
	}

	@GetMapping("/postForEntity")
	public CommonResult<Payment> addPayment2(Payment payment) {
		log.info("[=插入数据=]{" + payment + "}");
		ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PAYMENT_URL + "/provider/addPayment", payment, CommonResult.class);
		if (entity.getStatusCode().is2xxSuccessful()) {
			//这个 实体对象 中还包含其他很多信息
			log.info("{状态码: " + entity.getStatusCode() +
					", 头信息: " + entity.getHeaders() + "}");
			// 如果成功
			return entity.getBody();
		} else {
			return new CommonResult<>(444, "【查询失败】");
		}
	}

	/**
	 * 用于测试自定义负载均衡
	 */
	@GetMapping(value = "/payment/lb")
	public String getPaymentLB() {
		// 根据 ServiceId 获取 Server 集合
		List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
		// 如果没有服务
		if (instances == null || instances.size() <= 0) {
			return null;
		}
		// 获取 Server 实例
		ServiceInstance serviceInstance = loadBalancer.instances(instances);
		// 通过实例获得该实例的 地址
		URI uri = serviceInstance.getUri();
		return restTemplate.getForObject(uri + "/provider/payment/lb", String.class);
	}

	// ====================> zipkin+sleuth
	@GetMapping("/payment/zipkin")
	public String paymentZipkin() {
		String result = restTemplate.getForObject(PAYMENT_URL + "/provider/payment/zipkin/", String.class);
		return result;
	}

}
